//
//  XYStatusesModel.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/15.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

class XYStatusesModel: NSObject {

    //微博信息内容
    var text:String?
    
    //微博ID
    var id:Int = 0
    
    //微博创建时间
    var created_at:String?
    
    //根据创建时间来显示的字符串
    var created_atStr:String{
        return NSDate.stringFormTime(created_at!)
    }
    
    //微博来源
    //"source": "<a href="http://weibo.com" rel="nofollow">新浪微博</a>",
    var source:String?{
        didSet{
            
            let sourceStr = source! as NSString
            let rangeL = sourceStr.rangeOfString("\">")
            
            if  rangeL.location != NSNotFound  {
                let rangeR = sourceStr.rangeOfString("</")
                if rangeR.location != NSNotFound{
                    //计算截取位置和截取长度
                    //位置
                    let loc = rangeL.location + rangeL.length
                    //长度
                    let len = rangeR.location - loc
                    
                    source = "来自 \(sourceStr.substringWithRange(NSMakeRange(loc, len)))"
                }
            }
        }
    }
    
    //转发数
    var reposts_count:Int = 0
    
    //评论数
    var comments_count:Int = 0
    
    //点赞
    var attitudes_count:Int = 0
    
    //微博配图(数组)
    var pic_urls:[XYPicModel]?
    
    //用户微博模型
    var user:XYUserStatusModel?
    
    //转发微博模型
    var retweeted_status:XYStatusesModel?
    
    init(dict:[String:AnyObject]){
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    //重写setvalue方法,给模型中的模型赋值
    override func setValue(value: AnyObject?, forKey key: String) {
        if (key as NSString).isEqualToString("user"){
            user = XYUserStatusModel(dict:(value  as! [String:AnyObject]))
            
        }else if(key as NSString).isEqualToString("retweeted_status"){
            retweeted_status = XYStatusesModel(dict: (value as! [String:AnyObject]))
            
        }else if(key as NSString).isEqualToString("pic_urls"){
            
            pic_urls = [XYPicModel]()
            if let v = (value as? [[String:AnyObject]]){
                for tempV in v{
                    let pic_M = XYPicModel(dict: tempV)
                    pic_urls?.append(pic_M)
                }
            }
            
        }else{
            super.setValue(value, forKey: key)
        }
    }
    
    //过滤掉多余的属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {  }
    
    
    
}
